package com.hulk.dryad.activiti7.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.hulk.dryad.activiti7.api.vo.ProcessInstanceVO;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.persistence.entity.ProcessInstanceModel;
import com.hulk.dryad.persistence.service.IProcessInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.activiti.api.model.shared.model.VariableInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 流程定义处理
 *
 * @author hulk
 * @date 2020-10-18 17:18:42
 */
@Api(value = "processInstance", tags = "流程实例模块")
@AllArgsConstructor
@RestController
@RequestMapping("/workflow/processInstance")
public class ProcessInstanceController {


	private IProcessInstanceService processInstanceService;

	@ApiOperation(value = "getPageInstance", notes = "获取流程实例分页信息")
	@PrintlnLog
	@GetMapping(value = "/getPageInstance")
	public R<IPage> getPageInstance(Page page, ProcessInstanceVO vo) {
		ProcessInstanceModel model = new ProcessInstanceModel();
		BeanUtils.copyProperties(vo, model);
		IPage<ProcessInstanceModel> ipage = processInstanceService.getProcessInstancePage(page, model);
		return R.ok(ipage);
	}


	@ApiOperation(value = "getInstance", notes = "通过实例ID获取流实例信息")
	@PrintlnLog
	@GetMapping(value = "/getInstance/{instanceId}")
	public R getInstance(@PathVariable("instanceId") String instanceId) {
		ProcessInstanceModel model = processInstanceService.getInstance(instanceId);
		return R.ok(model);
	}

	@ApiOperation(value = "startProcess", notes = "启动实例")
	@PrintlnLog
	@Idempotent
	@PostMapping (value = "/startProcess")
	public R startProcess(@RequestParam("processDefinitionKey") String processDefinitionKey,
						  @RequestParam("instanceName") String instanceName, @RequestParam("businessKey") String businessKey,
						  @RequestBody Map<String,Object> instanceVariables) {
		ProcessInstanceModel model = processInstanceService.startProcess(processDefinitionKey, instanceName, businessKey,
				instanceVariables);
		return R.ok(model);
	}

	@ApiOperation(value = "removeInstance", notes = "删除实例")
	@PrintlnLog
	@Idempotent
	@DeleteMapping(value = "/removeInstance/{instanceId}")
	public R removeInstance(@PathVariable("instanceId") String instanceId) {
		ProcessInstanceModel model = processInstanceService.removeInstance(instanceId);
		return R.ok(model);
	}


	@ApiOperation(value = "suspendInstance", notes = "挂起冷冻")
	@PrintlnLog
	@Idempotent
	@GetMapping(value = "/suspendInstance/{instanceId}")
	public R suspendInstance(@PathVariable("instanceId") String instanceId) {
		ProcessInstanceModel model = processInstanceService.suspendInstance(instanceId);
		return R.ok(model);
	}


	@ApiOperation(value = "resumeInstance", notes = "激活冷冻")
	@PrintlnLog
	@Idempotent
	@GetMapping(value = "/resumeInstance/{instanceId}")
	public R resumeInstance(@PathVariable("instanceId") String instanceId) {
		ProcessInstanceModel model = processInstanceService.activeInstance(instanceId);
		return R.ok(model);
	}



	@ApiOperation(value = "getVariables", notes = "获取参数")
	@PrintlnLog
	@GetMapping(value = "/variables/{instanceId}")
	public R getVariables(@PathVariable("instanceId") String instanceId) {
		List<VariableInstance> variables = processInstanceService.getVariables(instanceId);
		List<Map<String, Object>> variableMaps = variables.stream().map(v -> {
			Map<String, Object> map = Maps.newHashMap();
			map.put("name", v.getName());
			map.put("value", v.getValue());
			map.put("taskId", v.getTaskId());
			map.put("type", v.getType());
			map.put("ProcessInstanceId", v.getProcessInstanceId());
			return map;
		}).collect(Collectors.toList());
		return R.ok(variableMaps);
	}


}
